#
# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

SET(PRODUCT_NAME csconnect)

PROJECT(cpp_script_connector)

IF(COMMAND CMAKE_POLICY)
  CMAKE_POLICY(SET CMP0002 OLD)
  CMAKE_POLICY(SET CMP0003 OLD)
  CMAKE_POLICY(SET CMP0005 OLD)
ENDIF(COMMAND CMAKE_POLICY)

SET(LIB_SUFFIX ""
  CACHE STRING "Define suffix of directory name (32/64)"
  FORCE)
SET(EXEC_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}
  CACHE PATH  "Base directory for executables and libraries"
  FORCE)

# The following are directories where stuff will be installed to.
SET(BIN_INSTALL_DIR "${EXEC_INSTALL_PREFIX}/bin"
  CACHE PATH "The binary install dir (default prefix/bin)"
  FORCE)
SET(LIB_INSTALL_DIR "${EXEC_INSTALL_PREFIX}/lib${LIB_SUFFIX}"
  CACHE PATH "The subdirectory relative to the install prefix where libraries will be installed (default is /lib${LIB_SUFFIX})"
  FORCE)
SET(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include/csconnect"
  CACHE PATH "The subdirectory to the header prefix"
  FORCE)

SET(CSCONNECT_MAJOR_VERSION 0)
SET(CSCONNECT_MINOR_VERSION 9)
SET(CSCONNECT_MICRO_VERSION 0)
SET(CSCONNECT_VERSION \"${CSCONNECT_MAJOR_VERSION}.${CSCONNECT_MINOR_VERSION}.${CSCONNECT_MICRO_VERSION}\")
SET(CSCONNECT_EPOCH "-1.0")

# The library version number is different than package version.
# It's just a temporary value, because the API is not stable yet.
SET(CSCONNECT_LIB_VERSION "0.0.0")
SET(CSCONNECT_LIB_SOVERSION "0")

# This string is used in auto update request. It should be updated to the
# time of a release build is made. Its format is yymmdd-HHMMSS.
SET(CSCONNECT_VERSION_TIMESTAMP \"090106-000000\")

IF(NOT CMAKE_BUILD_TYPE)
  SET(CMAKE_BUILD_TYPE "Release")
ENDIF(NOT CMAKE_BUILD_TYPE)

IF("${CMAKE_BUILD_TYPE}" STREQUAL "debugfull"
    OR "${CMAKE_BUILD_TYPE}" STREQUAL "debug")
  SET(CMAKE_BUILD_TYPE "Debug")
ENDIF("${CMAKE_BUILD_TYPE}" STREQUAL "debugfull"
    OR "${CMAKE_BUILD_TYPE}" STREQUAL "debug")

# TODO: detect the actual platform.
SET(CSCONNECT_PLATFORM_SHORT \"linux\")
SET(CSCONNECT_PLATFORM \"linux\")

INCLUDE(CheckFunctionExists)
INCLUDE(FindPkgConfig)

INCLUDE(CheckTypeSize)
SET(HAVE_STDDEF_H 1)
CHECK_TYPE_SIZE(char CSCONNECT_SIZEOF_CHAR)
CHECK_TYPE_SIZE(short CSCONNECT_SIZEOF_SHORT_INT)
CHECK_TYPE_SIZE(int CSCONNECT_SIZEOF_INT)
CHECK_TYPE_SIZE(long CSCONNECT_SIZEOF_LONG_INT)
CHECK_TYPE_SIZE(size_t CSCONNECT_SIZEOF_SIZE_T)
CHECK_TYPE_SIZE(double CSCONNECT_SIZEOF_DOUBLE)
CHECK_TYPE_SIZE(wchar_t CSCONNECT_SIZEOF_WCHAR_T)

CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/csconnect/sysdeps.h.in
               ${CMAKE_BINARY_DIR}/csconnect/sysdeps.h)

IF("${CMAKE_BUILD_TYPE}" STREQUAL "Debug"
    AND EXISTS ${CMAKE_SOURCE_DIR}/CTestConfig.cmake)
  INCLUDE(Dart)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
  SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
ENDIF("${CMAKE_BUILD_TYPE}" STREQUAL "Debug"
    AND EXISTS ${CMAKE_SOURCE_DIR}/CTestConfig.cmake)

INCLUDE(CopyUtils)
INCLUDE(GenerateOutput)
INCLUDE(PkgConfigEx)
INCLUDE(TestSuite)

SET(prefix "${CMAKE_INSTALL_PREFIX}")
SET(exec_prefix "${EXEC_INSTALL_PREFIX}")
SET(libdir "${LIB_INSTALL_DIR}")
SET(includedir "${prefix}/include")
SET(datarootdir "${prefix}/share")
SET(datadir "${datarootdir}")
SET(sysconfdir "${prefix}/etc")

SET(CSCONNECT_INCLUDE_DIR "${CMAKE_INSTALL_PREFIX}/include/${PRODUCT_NAME}")
SET(CSCONNECT_SYSDEPS_INCLUDE_DIR "${LIB_INSTALL_DIR}/${PRODUCT_NAME}/include")

ADD_DEFINITIONS(
  -DUNIX
  -DPREFIX=${CMAKE_INSTALL_PREFIX}
  -DPRODUCT_NAME=${PRODUCT_NAME}
  # For stdint.h macros like INT64_C etc.
  -D__STDC_CONSTANT_MACROS)

INCLUDE(CheckCCompilerFlag)
IF(UNIX)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Werror -Wconversion")
  # No "-Wall -Werror" for C flags, to avoid third_party code break.
  SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
ENDIF(UNIX)
# SET(CMAKE_SKIP_RPATH ON)
ENABLE_TESTING()

IF("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
  ADD_DEFINITIONS(-D_DEBUG)
  CHECK_C_COMPILER_FLAG(-fstack-protector-all GCC_STACK_PROTECTOR_ALL)
  IF(GCC_STACK_PROTECTOR_ALL)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector-all")
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-all")
  ENDIF(GCC_STACK_PROTECTOR_ALL)
ELSE("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
  ADD_DEFINITIONS(-DNDEBUG)
  CHECK_C_COMPILER_FLAG(-fstack-protector GCC_STACK_PROTECTOR)
  IF(GCC_STACK_PROTECTOR)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector")
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
  ENDIF(GCC_STACK_PROTECTOR)
ENDIF("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")

INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR})

INCLUDE(SpiderMonkey)
IF(NOT MOZJS_FOUND)
  SET(CSCONNECT_BUILD_MOZJS_ADAPTER 0)
  MESSAGE("Library SpiderMonkey is not available, libcsconnect-mozjs won't be built.")
ELSE(NOT MOZJS_FOUND)
  SET(CSCONNECT_BUILD_MOZJS_ADAPTER 1)
ENDIF(NOT MOZJS_FOUND)

MESSAGE("
Build options:
  Version                          ${CSCONNECT_VERSION}
  Build type                       ${CMAKE_BUILD_TYPE}

 Script engine adapters:
  Build libcsconnect-mozjs         ${CSCONNECT_BUILD_MOZJS_ADAPTER}
")
